<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="激活码">
        <el-input v-model="queryParams.code"/>
      </el-form-item>
      <el-form-item label="创建人">
        <el-select v-model="queryParams.createUserId" clearable style="width: 200px" filterable>
          <el-option v-for="user in userList" :label="user.nickName" :value="user.userId" :key="user.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['work:activation:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAddAdmin"
            v-hasPermi="['work:activation:addAdmin']"
        >新增渠道激活码
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['work:activation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="激活码" align="center" prop="code"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <span v-if="scope.row.status == 1">未使用</span>
          <span v-if="scope.row.status == 2">已使用</span>
          <span v-if="scope.row.status == 3">已关闭</span>
        </template>
      </el-table-column>
      <el-table-column label="激活天数" align="center" prop="days"/>
      <el-table-column label="点数" align="center" prop="pointNum"/>
      <el-table-column label="创建人" align="center" prop="createUserName"/>
      <el-table-column label="使用人" align="center" prop="userName"/>
      <el-table-column label="使用时间" align="center" prop="usedTime"/>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改激活码对话框 -->
    <el-dialog title="新增激活码" v-model="openAdmin" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="渠道商" prop="createUserId">
              <el-select v-model="form.createUserId" clearable style="width: 200px" filterable>
                <el-option v-for="user in userList" :label="user.nickName" :value="user.userId" :key="user.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="激活码数量" prop="num">
              <el-input-number v-model="form.num"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="激活天数" prop="days">
              <el-input-number v-model="form.days"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="点数" prop="pointNum">
              <el-input-number v-model="form.pointNum"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div align="center">
          <el-button type="primary" @click="submitAdminForm">确 定</el-button>
          <el-button @click="openAdmin = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改激活码对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="激活码数量" prop="num">
              <el-input-number v-model="form.num"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="激活天数" prop="days">
              <el-input-number v-model="form.days"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="点数" prop="pointNum">
              <el-input-number v-model="form.pointNum"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div align="center">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {listActivation, getActivation, delActivation, addFastActivation, updateActivation} from "@/api/work/activation";
import {queryUser} from "../../../api/system/user.js";
import {addAdminActivation} from "../../../api/work/activation.js";

export default {
  name: "Activation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 激活码表格数据
      activationList: [],
      userList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createUserId: null,
        code: null,
        status: null,
        days: null,
        usedTime: null,
        userId: null
      },
      // 表单参数
      form: {},
      openAdmin:false,
      // 表单校验
      rules: {
        num: [
          {required: true, message: "数量必填", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    queryUser({userType:"00"}).then(response => {
      this.userList = response.rows;
    })
  },
  methods: {
    /** 查询激活码列表 */
    getList() {
      this.loading = true;
      listActivation(this.queryParams).then(response => {
        this.activationList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        num:1,
        days:30,
        pointNum:100
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加激活码";
    },
    /** 新增按钮操作 */
    handleAddAdmin() {
      this.reset();
      this.openAdmin = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0]
      getActivation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改激活码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addFastActivation(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitAdminForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addAdminActivation(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.openAdmin = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选择的数据项？').then(function () {
        return delActivation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/activation/export', {
        ...this.queryParams
      }, `activation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
